/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Serverend.java
 *
 * Created on 14 Aug, 2011, 3:16:32 PM
 */

package server;

import java.awt.TextField;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;

/**
 *
 * @author Pri
 */
public class Serverend extends javax.swing.JFrame {

    /** Creates new form Serverend */
    public Serverend() {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jTextField2 = new javax.swing.JTextField();
        Checkbalance = new javax.swing.JButton();
        Withdraw = new javax.swing.JButton();
        Deposit = new javax.swing.JButton();
        jLabel2 = new javax.swing.JLabel();
        Viewdetails = new javax.swing.JButton();
        Sendpassword = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 14));
        jLabel1.setText("My Bill Buddy");

        jTextField2.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jTextField2.setName("null"); // NOI18N
        jTextField2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTextField2ActionPerformed(evt);
            }
        });

        Checkbalance.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        Checkbalance.setText("Check Balance");
        Checkbalance.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                CheckbalanceActionPerformed(evt);
            }
        });

        Withdraw.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        Withdraw.setText("Withdraw");
        Withdraw.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                WithdrawActionPerformed(evt);
            }
        });

        Deposit.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        Deposit.setText("Deposit");
        Deposit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                DepositActionPerformed(evt);
            }
        });

        jLabel2.setFont(new java.awt.Font("Tahoma", 0, 14));
        jLabel2.setText("Phone Number");

        Viewdetails.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        Viewdetails.setText("View Details");
        Viewdetails.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ViewdetailsActionPerformed(evt);
            }
        });

        Sendpassword.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        Sendpassword.setText("Send Password");
        Sendpassword.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                SendpasswordActionPerformed(evt);
            }
        });

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .add(255, 255, 255)
                .add(jLabel1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 93, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(285, Short.MAX_VALUE))
            .add(layout.createSequentialGroup()
                .add(253, 253, 253)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(org.jdesktop.layout.GroupLayout.TRAILING, Deposit, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 124, Short.MAX_VALUE)
                    .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                            .add(org.jdesktop.layout.GroupLayout.LEADING, Checkbalance, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 121, Short.MAX_VALUE)
                            .add(Withdraw, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 121, Short.MAX_VALUE))
                        .add(3, 3, 3)))
                .add(256, 256, 256))
            .add(layout.createSequentialGroup()
                .add(198, 198, 198)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                    .add(Sendpassword, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 161, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(layout.createSequentialGroup()
                        .add(jLabel2)
                        .add(34, 34, 34)
                        .add(jTextField2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 125, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 50, Short.MAX_VALUE)
                .add(Viewdetails)
                .add(31, 31, 31))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(jLabel1)
                .add(30, 30, 30)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jTextField2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(jLabel2)
                    .add(Viewdetails))
                .add(38, 38, 38)
                .add(Sendpassword)
                .add(18, 18, 18)
                .add(Checkbalance)
                .add(18, 18, 18)
                .add(Withdraw)
                .add(18, 18, 18)
                .add(Deposit)
                .addContainerGap(22, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jTextField2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTextField2ActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jTextField2ActionPerformed

    private void DepositActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_DepositActionPerformed
        // TODO add your handling code here:        
        String Number =jTextField2.getText();
        DB database = new DB();
        if (Number.length()==0)
        {
            JOptionPane.showMessageDialog(null, "Please enter the phone number", "Error", JOptionPane.PLAIN_MESSAGE);
        }
        else
        {
            int val =0;
            ResultSet result = database.select("Select phone_number from users");
            try {
                while(result.next())
                {
                    if(result.getString(1).equalsIgnoreCase(Number))
                        val =1;
                    else val=0;
                }
            } catch (SQLException ex) {
                JOptionPane.showMessageDialog(null, "Cannot access database", "Error", JOptionPane.PLAIN_MESSAGE);
            }
                
            if(val==1)
            {
            JTextField amt = new JTextField();
            final JComponent[] input1 = new JComponent[]
            {
                new JLabel("Enter the amount you want to deposit: "),amt
            };
            JOptionPane.showMessageDialog(null, input1, "Amount", JOptionPane.PLAIN_MESSAGE);
            if(amt.getText().length() == 0)
            {
                JOptionPane.showMessageDialog(null, "Please enter the amount", "Error", JOptionPane.PLAIN_MESSAGE);
            }
            else
            {
            int amount = Integer.parseInt(amt.getText());
                        int balance = 0;
            //System.out.println(amount);
              try {
            ResultSet bal = database.select("Select balance from users where phone_number =" + '"' + Number + '"');
                
            while (bal.next())
            {
            balance = bal.getInt(1);
            }
            } catch (SQLException ex) {
                  JOptionPane.showMessageDialog(null, "Cannot access database", "Error", JOptionPane.PLAIN_MESSAGE);
            }
            balance = balance + amount;
            int value = database.update("Update users set balance =" + '"' +  balance + '"' + "where phone_number=" + '"' + Number + '"');

            
            try{
            ResultSet bal = database.select("Select balance from users where phone_number =" + '"' + Number + '"');
                
            while (bal.next())
            {
            balance = bal.getInt(1);
                JOptionPane.showMessageDialog(null, "Current Balance now is " + balance, "Message", JOptionPane.PLAIN_MESSAGE);
            }
            } catch (SQLException ex) {
                JOptionPane.showMessageDialog(null, "Cannot access database", "Error", JOptionPane.PLAIN_MESSAGE);
            }
                    }
            }
            else {
                JOptionPane.showMessageDialog(null, "Invalid Phone Number", "Error", JOptionPane.PLAIN_MESSAGE);
            }
        }
        

        
        
    }//GEN-LAST:event_DepositActionPerformed

    private void CheckbalanceActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_CheckbalanceActionPerformed
        // TODO add your handling code here:        
        String Number =jTextField2.getText();
        DB database = new DB();
        if (Number.length()==0)
        {
            JOptionPane.showMessageDialog(null, "Please enter the phone number", "Error", JOptionPane.PLAIN_MESSAGE);
        }
        else
        {
            int val =0;
            ResultSet result = database.select("Select phone_number from users");
            try {
                while(result.next())
                {
                    if(result.getString(1).equalsIgnoreCase(Number))
                        val =1;
                    else val=0;
                }
            } catch (SQLException ex) {
                JOptionPane.showMessageDialog(null, "Cannot access database", "Error", JOptionPane.PLAIN_MESSAGE);
            }
                
            if(val==1)
            {
            try{
            ResultSet bal = database.select("Select balance from users where phone_number =" + '"' + Number + '"');
                
            while (bal.next())
            {
            int balance = bal.getInt(1);
                JOptionPane.showMessageDialog(null, "Current Balance now is " + balance, "Message", JOptionPane.PLAIN_MESSAGE);
            }
            } catch (SQLException ex) {
                JOptionPane.showMessageDialog(null, "Cannot access database", "Error", JOptionPane.PLAIN_MESSAGE);
            }
                    }
            else {
                JOptionPane.showMessageDialog(null, "Invalid Phone Number", "Error", JOptionPane.PLAIN_MESSAGE);
            }
        }
        
    }//GEN-LAST:event_CheckbalanceActionPerformed

    private void WithdrawActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_WithdrawActionPerformed
        // TODO add your handling code here:        
        String Number =jTextField2.getText();
        DB database = new DB();
        if (Number.length()==0)
        {
            JOptionPane.showMessageDialog(null, "Please enter the phone number", "Error", JOptionPane.PLAIN_MESSAGE);
        }
        else
        {
            int val =0;
            ResultSet result = database.select("Select phone_number from users");
            try {
                while(result.next())
                {
                    if(result.getString(1).equalsIgnoreCase(Number))
                        val =1;
                    else val=0;
                }
            } catch (SQLException ex) {
               JOptionPane.showMessageDialog(null, "Cannot access database", "Error", JOptionPane.PLAIN_MESSAGE);
            }
                
            if(val==1)
            {
            JTextField amt = new JTextField();
            final JComponent[] input1 = new JComponent[]
            {
                new JLabel("Enter the amount you want to withdraw: "),amt
            };
            JOptionPane.showMessageDialog(null, input1, "Amount", JOptionPane.PLAIN_MESSAGE);
            if(amt.getText().length()==0)
            {
                JOptionPane.showMessageDialog(null, "Please enter the amount", "Error", JOptionPane.PLAIN_MESSAGE);
            }
            else
            {
            int amount = Integer.parseInt(amt.getText());
                        int balance = 0;
            //System.out.println(amount);
              try {
            ResultSet bal = database.select("Select balance from users where phone_number =" + '"' + Number + '"');
                
            while (bal.next())
            {
            balance = bal.getInt(1);
            }
            } catch (SQLException ex) {
                JOptionPane.showMessageDialog(null, "Cannot access database", "Error", JOptionPane.PLAIN_MESSAGE);
            }
              int value;
            balance = balance - amount;
            if(balance<=0)
                JOptionPane.showMessageDialog(null, "Cannot withdraw, balance not sufficient", "Error", JOptionPane.PLAIN_MESSAGE);
            else
            value = database.update("Update users set balance =" + '"' +  balance + '"' + "where phone_number=" + '"' + Number + '"');

            
            try{
            ResultSet bal = database.select("Select balance from users where phone_number =" + '"' + Number + '"');
                
            while (bal.next())
            {
            balance = bal.getInt(1);
                JOptionPane.showMessageDialog(null, "Current Balance now is " + balance, "Message", JOptionPane.PLAIN_MESSAGE);
            }
            } catch (SQLException ex) {
                JOptionPane.showMessageDialog(null, "Cannot access database", "Error", JOptionPane.PLAIN_MESSAGE);
            }
                    }
                        
            }
            else {
                JOptionPane.showMessageDialog(null, "Invalid Phone Number", "Error", JOptionPane.PLAIN_MESSAGE);
            }
        }
        
    }//GEN-LAST:event_WithdrawActionPerformed

    private void ViewdetailsActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ViewdetailsActionPerformed
        // TODO add your handling code here:
        String Number =jTextField2.getText();
        DB database = new DB();
        if (Number.length()==0)
        {
            JOptionPane.showMessageDialog(null, "Please enter the phone number", "Error", JOptionPane.PLAIN_MESSAGE);
        }
         else
        {
            int val =0;
            ResultSet result = database.select("Select phone_number from users");
            try {
                while(result.next())
                {
                    if(result.getString(1).equalsIgnoreCase(Number))
                        val =1;
                    else val=0;
                }
            } catch (SQLException ex) {
                JOptionPane.showMessageDialog(null, "Cannot access database", "Error", JOptionPane.PLAIN_MESSAGE);
            }
            if(val==1)
            {
                        try{
            ResultSet res = database.select("Select iiitd_email, name, phone_number from users where phone_number =" + '"' + Number + '"');
                
            while (res.next())
            {
                JOptionPane.showMessageDialog(null, "IIIT-D email: " + res.getString(1) + "\nName: " + res.getString(2) + "\nPhone Number: "+ res.getString(3), "Message", JOptionPane.PLAIN_MESSAGE);
            }
            } catch (SQLException ex) {
                JOptionPane.showMessageDialog(null, "Cannot access database", "Error", JOptionPane.PLAIN_MESSAGE);
            }
            }
            else
            {
                JOptionPane.showMessageDialog(null, "Invalid Phone Number", "Error", JOptionPane.PLAIN_MESSAGE);
            }
        }
        
    }//GEN-LAST:event_ViewdetailsActionPerformed

    private void SendpasswordActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_SendpasswordActionPerformed
        // TODO add your handling code here:
        String Number =jTextField2.getText();
        DB database = new DB();
        if (Number.length()==0)
        {
            JOptionPane.showMessageDialog(null, "Please enter the phone number", "Error", JOptionPane.PLAIN_MESSAGE);
        }
        else
        {
            int val =0;
            ResultSet result = database.select("Select phone_number from users");
            try {
                while(result.next())
                {
                    if(result.getString(1).equalsIgnoreCase(Number))
                        val =1;
                    else val=0;
                }
            } catch (SQLException ex) {
                JOptionPane.showMessageDialog(null, "Cannot access database", "Error", JOptionPane.PLAIN_MESSAGE);
            }
            if(val==1)
            {
                
            }
            else
            {
                JOptionPane.showMessageDialog(null, "Invalid Phone Number", "Error", JOptionPane.PLAIN_MESSAGE);
            }
        }
        
    }//GEN-LAST:event_SendpasswordActionPerformed

    /**
    * @param args the command line arguments
    */


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton Checkbalance;
    private javax.swing.JButton Deposit;
    private javax.swing.JButton Sendpassword;
    private javax.swing.JButton Viewdetails;
    private javax.swing.JButton Withdraw;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JTextField jTextField2;
    // End of variables declaration//GEN-END:variables

}
